Skip to content

Latest commit

 

History

History
50 lines (43 loc) · 1.47 KB

File metadata and controls

50 lines (43 loc) · 1.47 KB

20230903132502 20230903132515

코드

import java.util.*;

class Solution {
    public int solution(int[] A, int[] B) {
        int answer = 0;
        
        Arrays.sort(A);
        Arrays.sort(B);
        ArrayList<Integer> list=new ArrayList<>();
        for(int i=0; i<B.length; i++){
            list.add(B[i]);
        }
        
        for(int i=0; i<A.length; i++){
            int cur=A[i];
            
            // 이분탐색을 통해
            // 주어진 값보다 큰 값이 처음으로 등장하는 것을 start에 저장
            int start=0;
            int end=list.size();
            while(start<end){
                int mid=(start+end)/2;
                
                if(list.get(mid)>cur){
                    end=mid;
                }
                else{
                    start=mid+1;
                }
            }
            
            // list에서 start번째 값 삭제
            if(start<list.size()){
                answer+=1;
                list.remove(start);
            }
            // cur보다 큰 값이 없으면 더 이상 비교할 필요 없으므로 종료
            else if(start==list.size()){
                break;
            }   
        }
        return answer;
    }
}